package com.tgpms.web.earlierStage.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.entity.ObsInfo;
import com.tgpms.web.earlierStage.service.ObsInfoService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ObsInfoController
 * @Description TODO
 * @Author GUAN
 * @Date 2019-12-24 16:24
 * @Version 1.0
 * @return
 **/
@Api(value = "ObsInfoController", tags = "前期 组织机构")
@RestController
@RequestMapping(value = "/ev/obsInfo")
public class ObsInfoController {
	
	private final Logger log = LoggerFactory.getLogger(ObsInfoController.class);

    @Autowired
    private TokenService tokenService;
    @Autowired
    private ObsInfoService obsInfoService;

    @ApiOperation(value = "查询树形组织机构接口", notes = "查询树形组织机构接口", httpMethod = "POST")
    @PostMapping(value = "/selObsInfoForTree")
    public Result selObsInfoForTree(@RequestParam(value = "usersId",required = false) String usersId,
    								@RequestParam(value = "obsCode",required = false) String obsCode,
    								@RequestParam(value = "obsNo",required = false) String obsNo) {
    	log.info("======查询树形组织机构接口开始======");
        Result result = new Result();
        List<JSONObject> list = new ArrayList<JSONObject>();
        try {
            list = obsInfoService.selObsInfo(usersId,obsCode,obsNo);
            result.setMsg(ExceptionExplain.SUCCESS_BY_INFOS.getExplain());
            result.setData(list);
        } catch (Exception e) {
        	e.printStackTrace();
            log.error("查询树形组织机构接口"+e);
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
        }
        log.info("======查询树形组织机构接口结束======");
        return result;
    }

    
    @ApiOperation(value = "新增项目查询树形组织机构接口", notes = "新增项目查询树形组织机构接口", httpMethod = "POST")
    @PostMapping(value = "/selObsInfoForTreeToAdd")
    public Result selObsInfoForTreeToAdd(@RequestParam(value = "usersId",required = false) String usersId) {
    	log.info("======新增项目查询树形组织机构接口开始======");
        Result result = new Result();
        List<Object> list = new ArrayList<>();
        if(Common.isEmpty(usersId)) {
        	result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
        }
        try {
            list = obsInfoService.selObsInfoToAdd(usersId);
            result.setMsg(ExceptionExplain.SUCCESS_BY_INFOS.getExplain());
            result.setData(list);
        } catch (Exception e) {
        	log.error("新增项目查询树形组织机构接口"+e);
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
        }
        log.info("======新增项目查询树形组织机构接口结束======");
        return result;
    }

    @ApiOperation(value = "新增组织机构接口", notes = "新增组织机构接口", httpMethod = "POST")
    @PostMapping(value = "/addObsInfo")
    public Result addObsInfo(@RequestBody ObsInfo obsInfo, HttpServletRequest request) {
        Result result = new Result();
        if(Common.isEmpty(obsInfo.getDescription())) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            String insertName = tokenService.getUserName(request);
            obsInfo.setInsertUser(insertName);
            result = obsInfoService.addObsInfo(obsInfo, result);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
        }
        return result;
    }


    @ApiOperation(value = "修改组织机构接口", notes = "修改组织机构接口", httpMethod = "PUT")
    @PutMapping(value = "/updateObsInfo")
    public Result updateObsInfo(@RequestBody ObsInfo obsInfo,HttpServletRequest request) {
        Result result = new Result();
        if(Common.isEmpty(obsInfo.getObsCode())) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
            return result;
        }
        try {
            String insertName = tokenService.getUserName(request);
            obsInfo.setModifyUser(insertName);
            result = obsInfoService.updateObsInfo(obsInfo, result);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
        }
        return result;
    }

    @ApiOperation(value = "删除组织机构接口", notes = "删除组织机构接口", httpMethod = "DELETE")
    @DeleteMapping(value = "/deleteObsInfo")
    public Result deleteObsInfo(@RequestParam(value = "obsCode", required = true) String obsCode,HttpServletRequest request) {
        Result result = new Result();
        if(Common.isEmpty(obsCode)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
            return result;
        }
        try {
            result = obsInfoService.deleteObsInfo(obsCode, result);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
        }
        return result;
    }
}
